var mobile = getParameter("mobile");
var from_center =parseInt(getParameter("from_center"));//来自个人中心
var from_partner =parseInt(getParameter("from_partner"));//来自合伙人界面
var partner_id =parseInt(getParameter("partner_id"));//合伙人的邀请人id，0代表平台，注册一级合伙人，//直接从公众号菜单过来时，partner_id=-1
var grade =parseInt(getParameter("grade"));
var user=getUserEntityFromCache();
var countdown=60;
var timer;
var isRequest=0;

function initData() {
    if(isNaN(from_center)) {
        from_center=0;
    }

    if(isNaN(from_partner)) {
        from_partner=0;
    }

    if(isNaN(partner_id))
    {
        partner_id=0;
    }

    if(isNaN(grade))
    {
        grade=0;
    }

    if(mobile!=undefined&&mobile!=null&&mobile.length>0){
        $("#codeNum").html("验证码已发送至手机 "+mobile);
    }
    else {
        $("#codeNum").html("发送失败，异常手机号");
    }
}

function checkParameter() {
    if(mobile == undefined || mobile == null || mobile.length <11){
        showAlert("对不起,手机号不合法");
        return false;
    }
    else if(user==undefined||user==null||!user.isLogin()){
        showAlert("对不起,没有您的授权信息");
        return false;
    }

    return true;
}

function addEvent() {
    $("#inputnum").keyup(function () {
        var inputStr = $.trim($("#inputnum").val());

        if ($("#num_" + (inputStr.length + 1)).text().length > 0) {
            $("#num_" + (inputStr.length + 1)).text("");
        }
        $("#num_"+inputStr.length).text(inputStr.substr(inputStr.length-1,1));

        if(inputStr.length>=6){
            bindMobileToWx();
        }
    });

    $("#inputnum").keydown(function () {
        var inputStr=$.trim($("#inputnum").val());
        if(inputStr.length>6){
            $("#inputnum").val(inputStr.substr(0,6));
        }
        if ($("#num_" + (inputStr.length + 1)).text().length > 0) {
            $("#num_" + (inputStr.length + 1)).text("");
        }
        $("#num_"+inputStr.length).text(inputStr.substr(inputStr.length-1,1));
    });

    $("#again_code span").click(function () {
        if(parseInt($(this).attr("data-enable"))>0){
            getMsgCode(mobile);
        }
    });

    $("#inputnum").trigger("click");

}


function bindMobileToWx() {
    if(checkParameter()&&isRequest==0){
        postBindMobileInfo();
    }
}

function postBindMobileInfo(){
    var inputStr = $.trim($("#inputnum").val());
    if(partner_id<0){
        partner_id=0;
    }
    $.ajax({
        type: "post",
        async: true,
        url: domain+"/v1/wx-user/bind-mobile",
        data: {mobile:mobile, uid:user.uid,sms_code:inputStr,referee:partner_id},
        dataType: "json",
        beforeSend:function(){
            isRequest=1;
            showLoading();
        },
        success: function(data){
            isRequest=0;
            hideLoading();

            if(data.code=="success"){
                //验证完手机务必把本地的user信息中的手机信息设置上去
                user.mobile=mobile;
                user.cacheUserInfo(user.openid,user.nickname,user.mobile,user.avatar,user.uid,user.role,partner_id,user.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
                localStorage.setItem("needshowQR","1");
                showAlert("绑定成功");

                if(parseInt(from_center)>0){
                    window.location.href="/user/index";
                }
                else {
                    if(parseInt(from_partner)>0){
                        window.location.href="/home/partner?grade="+grade+"&partner_id="+partner_id;
                    }
                    else {
                        window.location.href="/home";
                    }
                }

            }
            else {
                showAlert(data.message);
            }
        },
        error: function(){
            isRequest=0;
            hideLoading();
        }
    });
}


function getMsgCode(mobile){
    if(mobile!=undefined&&mobile!=null&&mobile.length>0){
        $.ajax({
            type: "post",
            async: true,
            url: "/v1/wx-user/send-msg",
            data: {mobile:mobile,uid:user.uid},
            dataType: "json",
            success: function(data){
                if(data&&data.code&&data.code=="success"){
                    var vcode=data.sms_code;
                    setTimer();
                    showAlert("验证码已发送");
                }
                else {
                    showAlert(data.message);
                }
            },
            error: function(){
                showAlert("请求失败");
            }
        });
    }
};


function setTimer() {
    if (countdown <= 0) {
        $("#again_code span").html("<em>重新获取</em>");
        $("#again_code span").attr("data-enable","1");

        clearTimeout(timer);
        countdown = 60;
    }
    else {
        countdown--;
        $("#again_code span").attr("data-enable","0");
        $("#again_code span").html("<em>"+countdown+"s</em> 后可以重新获取");
    }
    if(countdown!=60){
        timer=setTimeout(function() {
            setTimer();
        },1000);
    }
}

$(function () {
    initData();
    addEvent();
    setTimer();
});

